<template>
  <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets" style="margin-top: 5px"></i>
      <span style="margin-top: 5px">数据列表</span>
      <el-button
        class="btn-add"
        @click="handleCreateOrder()"
        style="float:right"
        size="mini">
        一键下单
      </el-button>
    </el-card>

    <el-table
      :key="tableKey"
      style="width: 100%;margin-top:20px;"
      :data="list"
      @selection-change="handleSelectionChange"
      v-loading="listLoading" border>
      <el-table-column type="selection" width="60" align="center"></el-table-column>
      <el-table-column label="编号" width="100" align="center">
        <template slot-scope="scope">{{scope.row.id}}</template>
      </el-table-column>
      <el-table-column label="名称" align="center">
        <template slot-scope="scope">{{scope.row.electronic.name}}</template>
      </el-table-column>
      <el-table-column label="封装" width="100" align="center">
        <template slot-scope="scope">{{scope.row.electronic.package}}</template>
      </el-table-column>
      <el-table-column label="价格/规格" width="100" align="center">
        <template slot-scope="scope">{{scope.row.electronic.price}}/{{scope.row.electronic.spec}}</template>
      </el-table-column>
      <el-table-column label="库存" width="100" align="center">
        <template slot-scope="scope">{{scope.row.electronic.stock}}</template>
      </el-table-column>
      <el-table-column label="所需数量" width="100" align="center">
        <template slot-scope="scope">{{scope.row.count}}</template>
      </el-table-column>
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="batch-operate-container" style="margin-top:20px">
      <el-select
        size="small"
        v-model="operateType" placeholder="批量操作">
        <el-option
          v-for="item in operateOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="small">
        确定
      </el-button>
      <el-pagination
        background
        style="float:right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.page"
        :page-size="listQuery.limit"
        :page-sizes="[5,10,15]"
        :total="total">
      </el-pagination>
    </div>

    <el-dialog
      title="在线下单"
      :visible.sync="dialogOrderVisiable"
      width="40%">
      <el-form ref="orderForm":model="orderForm" :rules="rules" label-width="120px">
        <el-form-item label="订单类型" prop="type">
          <el-select size="small" v-model="orderForm.type">
            <el-option
              v-for="item in orderTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="下单数量" prop="count">
          <el-input v-model="orderForm.count" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="orderForm.note" type="textarea" :rows="2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogOrderVisiable = false">取 消</el-button>
        <el-button type="primary" @click="handleOrderConfirm('orderForm')">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>

import {
  fetchElectronicBoomItemList,
  updateElectronicBoomItem,
  deleteElectronicBoomItem
} from '@/api/electronicBoom'
import {
  createElectronicOrder
} from '@/api/electronicOrder'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: "electronicBoom",
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      list: null,
      listLoading: true,
      total: 10,
      electronicBoomId: null,
      operateType: null,
      listQuery: {
        page: 1,
        limit: 10
      },
      operateOptions: [
        {
          label: "批量删除",
          value: 1
        },
        {
          label: "批量下单",
          value: 2
        }
      ],
      multipleSelection: [],
      dialogOrderVisiable: false,
      orderTypeOptions: [
        {
          label: "入库",
          value: 1
        },
        {
          label: "出库",
          value: 2
        },
        {
          label: "采购",
          value: 3
        }
      ],
      orderForm: {
        type: null,
        count: 1,
        note: '',
        electronic_ids: null,
        electronic_cnts: null
      },
      rules: {
        type: [
          { required: true, message: '请选择订单类型', trigger: 'blur' }
        ],
        count: [
          { required: true, message: '请填写数量', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.electronicBoomId = this.$route.query.id
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      fetchElectronicBoomItemList(this.electronicBoomId, this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        this.listLoading = false
      });
    },
    handleBatchOperate(){
      if(this.multipleSelection==null||this.multipleSelection.length<1){
        this.$message({
          message: '请选择要操作的元器件',
          type: 'warning',
          duration: 1000
        });
        return;
      }
      if(this.operateType===1){
        //删除
        let ids=[];
        for(let i=0;i<this.multipleSelection.length;i++){
          ids.push(this.multipleSelection[i].id);
        }
        this.deleteItem(ids);
      }else if(this.operateType===2){
      }
    },
    handleSizeChange(val){
      this.listQuery.page = 1;
      this.listQuery.limit = val;
      this.fetchData();
    },
    handleCurrentChange(val){
      this.listQuery.page = val;
      this.fetchData();
    },
    handleSelectionChange(val){
      this.multipleSelection = val;
    },
    handleDelete(row) {
      this.$confirm('是否要从当前boom单中移除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteElectronicBoomItem(row.id).then(response => {
          this.$message(response.message)
          this.fetchData()
        });
      });
    },
    handleUpdate(row) {
    },
    deleteItem(ids) {
      this.$confirm('是否要进行该删除操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(ids)
        deleteElectronicBoomItem(ids).then(response=>{
          this.$message(response.message)
          this.fetchData();
        });
      })
    },
    handleCreateOrder(row) {
      // 创建订单
      this.dialogOrderVisiable = true
    },
    handleOrderConfirm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let ids=[];
          let cnts=[];
          for(let i=0;i<this.list.length;i++){
            ids.push(this.list[i].electronic.id);
            cnts.push(this.list[i].count*this.orderForm.count);
          }
          this.orderForm.electronic_ids = ids
          this.orderForm.electronic_cnts = cnts
          createElectronicOrder(this.orderForm).then(response=>{
            this.$message(response.message)
            this.dialogOrderVisiable = false
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>

</style>
